home *** CD-ROM | disk | FTP | other *** search
/ United Public Domain Gold 2 / United Public Domain Gold 2.iso / utilities / pu086.dms / pu086.adf / tools / cc.c < prev    next >
C/C++ Source or Header  |  1990-12-02  |  7KB  |  353 lines

  1. /*
  2.   (c) 1990 S.Hawtin.
  3.   Permission is granted to copy this file provided that:
  4.    1) It is not used for commercial gain
  5.    2) This notice is included in all copies
  6.    3) Altered copies are marked as such.
  7.  
  8.   No liability is accepted for the contents of the file.
  9.  
  10.   cc.c        within        CC
  11.  
  12. */
  13.  
  14. #include <stdio.h>
  15. #include <string.h>
  16.  
  17. extern long system();
  18.  
  19. /* Deal with at most 32 files at a time */
  20. #define MAXFILES 32
  21. #define MAXOBJS  64
  22. #define MAXFLAG  16
  23.  
  24. int just_testing=0;
  25.  
  26. /* Array of suffixes for 'C' files */
  27. char *Csuffix[]={".c",".i",NULL};
  28. /* Options NorthC understands */
  29. char *Copts="iIdDuU";
  30. /* Array of filenames */
  31. char *Cfiles[MAXFILES];
  32. int Ccount = 0;
  33. /* Flags for NorthC */
  34. char *Cflags[MAXFLAG];
  35. int Cfcount = 0;
  36. /* Command name */
  37. #define Ccmnd "NorthC"
  38.  
  39. /* Assembler files */
  40. char *Ssuffix[]={".s",".asm",NULL};
  41. char *Sopts="iI";
  42. char *Sfiles[MAXFILES];
  43. int Scount = 0;
  44. char *Sflags[MAXFLAG];
  45. int Sfcount = 0;
  46. #define Scmnd "a68k -g"
  47.  
  48. /* Object files */
  49. char *Osuffix[]={".o"};
  50. char *Ofiles[MAXOBJS];
  51. int Ocount = 0;
  52. char *Oflags[MAXFLAG];
  53. int Ofcount = 0;
  54. #define Ocmnd "blink"
  55.  
  56. /* Libraries */
  57. char *Lfiles[16];
  58. int Lcount=0;
  59.  
  60. /* Array of option strings */
  61. char *option_str[20];
  62. int ocount = 0;
  63.  
  64. char exe_name[32]="a.out";
  65. char library[32] ="clibs:";
  66.  
  67. add_suffix(str,suffix)
  68.     char *str;
  69.     char *suffix;
  70.    {/* Add suffix, replacing the old one */
  71.     /* garenteed to have a '.' */
  72.  
  73.     strcpy(strrchr(str,'.'),suffix);
  74.     }
  75.  
  76. tidy(files,count)
  77.     char **files;
  78.     int count;
  79.    {/* Tidy up any generated files */
  80.     int i;
  81.     for(i=0;i<count;i++)
  82.        {if(files[i][0]=='G')
  83.            {remove(&files[i][1]);
  84.             }
  85.         }
  86.     }
  87.  
  88. add_opts(cmnd_str,opt_list,flag_list,flag_count)
  89.     char *cmnd_str;
  90.     char *opt_list;
  91.     char **flag_list;
  92.     int  flag_count;
  93.    {
  94.     int i;
  95.  
  96.     for(i=0;i<=flag_count;i++)
  97.        {strcat(cmnd_str," ");
  98.         strcat(cmnd_str,flag_list[i]);
  99.         }
  100.  
  101.     for(i=0;i<ocount;i++)
  102.         if(strchr(opt_list,option_str[2]))
  103.            {/* copy each option string */
  104.             strcat(cmnd_str," ");
  105.             strcat(cmnd_str,option_str[i]);
  106.             }
  107.     }
  108.  
  109. /* Convert between file types */
  110.  
  111. ctos()
  112.    {/* Compile c sources or preprocessed files into assembler */
  113.     char cmnd_str[256];
  114.     int  count,i;
  115.     long ret;
  116.  
  117.     for(count=0;count<Ccount;count++)
  118.        {/* Compile each file in turn */
  119.  
  120.     /* Work out the output file name */
  121.     Sfiles[Scount]=malloc(strlen(Cfiles[count])+1);
  122.     strcpy(Sfiles[Scount],"G");
  123.     strcat(Sfiles[Scount],&Cfiles[count][1]);
  124.     add_suffix(Sfiles[Scount],Ssuffix[0]);
  125.  
  126.     /* Now make up the compiler command */
  127.     strcpy(cmnd_str,Ccmnd);
  128.  
  129.     add_opts(cmnd_str,Copts,Cflags,Cfcount);
  130.  
  131.     strcat(cmnd_str," -O");
  132.     strcat(cmnd_str,&Sfiles[Scount++][1]);
  133.     strcat(cmnd_str," ");
  134.     strcat(cmnd_str,&Cfiles[count][1]);
  135.  
  136.     if(just_testing)
  137.         printf("Command |%s|\n",cmnd_str);
  138.           else
  139.           ret = system(cmnd_str);
  140.         }
  141.     }
  142.  
  143. stoo()
  144.    {/* Assemble the assembly files */
  145.     char cmnd_str[256];
  146.     int  count,i;
  147.     long ret;
  148.  
  149.     for(count=0;count<Scount;count++)
  150.        {/* Assemble each file in turn */
  151.  
  152.     /* Work out the output file name */
  153.     Ofiles[Ocount]=malloc(strlen(Sfiles[count])+1);
  154.     strcpy(Ofiles[Ocount],"G");
  155.     strcat(Ofiles[Ocount],&Sfiles[count][1]);
  156.     add_suffix(Ofiles[Ocount],Osuffix[0]);
  157.  
  158.     /* Now make up the command */
  159.     strcpy(cmnd_str,Scmnd);
  160.     add_opts(cmnd_str,Sopts,Sflags,Sfcount);
  161.  
  162.     strcat(cmnd_str," -O");
  163.     strcat(cmnd_str,&Ofiles[Ocount++][1]);
  164.     strcat(cmnd_str," ");
  165.     strcat(cmnd_str,&Sfiles[count][1]);
  166.  
  167.     if(just_testing)
  168.         printf("Command |%s|\n",cmnd_str);
  169.           else
  170.           ret = system(cmnd_str);
  171.         }
  172.     tidy(Sfiles,Scount);
  173.     }
  174.  
  175. otoexe()
  176.    {/* Link the *.o programs to create the executable */
  177.     char cmnd_str[512];
  178.     int i;
  179.     long ret;
  180.  
  181.     strcpy(cmnd_str,Ocmnd);
  182.     strcat(cmnd_str," ");
  183.     strcat(cmnd_str,library);
  184.     strcat(cmnd_str,"crt0.o");
  185.  
  186.     for(i=0;i<Ocount;i++)
  187.        {strcat(cmnd_str," ");
  188.     strcat(cmnd_str,&Ofiles[i][1]);
  189.     }
  190.     add_opts(cmnd_str,"",Oflags,Ofcount);
  191.  
  192.     strcat(cmnd_str," TO ");
  193.     strcat(cmnd_str,exe_name);
  194.     strcat(cmnd_str," LIB ");
  195.     for(i=0;i<Lcount;i++)
  196.        {strcat(cmnd_str,library);
  197.     strcat(cmnd_str,"lib");
  198.         strcat(cmnd_str,Lfiles[i]);
  199.         strcat(cmnd_str,".a ");
  200.         }
  201.     strcat(cmnd_str,library);
  202.     strcat(cmnd_str,"libc.a");
  203.  
  204.     if(just_testing)
  205.         printf("Command |%s|\n",cmnd_str);
  206.       else
  207.         ret = system(cmnd_str);
  208.  
  209.     tidy(Ofiles,Ocount);
  210.     }
  211.  
  212. clasify(str)
  213.     char *str;
  214.    {/* Classify as C file */
  215.     char *temp,*file;
  216.     int  type,i;
  217.  
  218.     file = malloc(strlen(str)+2);
  219.     file[0] = 'O';
  220.     strcpy(&file[1],str);
  221.  
  222.     type = 2;
  223.     temp = strrchr(str,'.');
  224.     if(temp)
  225.        {for(i=0;Csuffix[i]!=NULL;i++)
  226.         if(strcmp(temp,Csuffix[i])==0)
  227.         type = 0;
  228.         for(i=0;Ssuffix[i]!=NULL;i++)
  229.         if(strcmp(temp,Ssuffix[i])==0)
  230.         type = 1;
  231.     }
  232.     
  233.     if(type==0)
  234.         Cfiles[Ccount++] = file;
  235.       else if (type==1)
  236.         Sfiles[Scount++] = file;
  237.       else
  238.         Ofiles[Ocount++] = file;
  239.     }
  240.  
  241. /* The main routine */
  242.  
  243. int justc = 0;
  244. int justs = 0;
  245.  
  246. do_c()
  247.    {justc = -1;
  248.     }
  249.  
  250. do_s()
  251.    {justs = -1;
  252.     }
  253.  
  254. do_l(lib)
  255.     char *lib;
  256.    {/* Add lib to the libraries */
  257.     Lfiles[Lcount++] = lib;
  258.     }
  259.  
  260. do_o(out)
  261.     char *out;
  262.    {/* change the output name */
  263.     strcpy(exe_name,out);
  264.     }
  265.  
  266. do_r(out)
  267.     char *out;
  268.    {/* Change the library directory */
  269.     strcpy(library,out);
  270.     }
  271.  
  272. do_quest()
  273.    {just_testing = 1;
  274.     }
  275.  
  276. do_a(out)
  277.     char *out;
  278.    {/* Add to assembler flags */
  279.     Sflags[Sfcount++]=out;
  280.     if(Sfcount>=MAXFLAG)
  281.        {printf("Too many A flags\n");
  282.     Sfcount--;
  283.         }
  284.     }
  285.  
  286. do_b(out)
  287.     char *out;
  288.    {/* Add to linker flags */
  289.     Oflags[Ofcount++]=out;
  290.     if(Ofcount>=MAXFLAG)
  291.        {printf("Too many B flags\n");
  292.     Ofcount--;
  293.         }
  294.     }
  295.  
  296. do_n(out)
  297.     char *out;
  298.    {/* Add to compiler flags */
  299.     Cflags[Cfcount++]=out;
  300.     if(Cfcount>=MAXFLAG)
  301.        {printf("Too many N flags\n");
  302.     Cfcount--;
  303.         }
  304.     }
  305.  
  306. struct _opts
  307.    {char ch;
  308.     int  (*fun)();
  309.     } Options[] = 
  310.    {{'a',do_a},
  311.     {'b',do_b},
  312.     {'c',do_c},
  313.     {'l',do_l},
  314.     {'n',do_n},
  315.     {'o',do_o},
  316.     {'r',do_r},
  317.     {'s',do_s},
  318.     {'?',do_quest},
  319.     {'\0'}};
  320.  
  321. main(argc,argv)
  322.     int argc;
  323.     char **argv;
  324.    {/* Handle cc commands */
  325.     int count;
  326.  
  327.     /* Scan the arguments, work out what to do */
  328.     for(count=1;count<argc;count++)
  329.        {/* If it starts with '-' it is a switch */
  330.         if(argv[count][0]!='-')
  331.            {/* Not a switch, add to the files */
  332.         clasify(argv[count]);
  333.             }
  334.           else
  335.            {int i;
  336.             for(i=0;Options[i].ch!='\0' && 
  337.                     Options[i].ch!=tolower(argv[count][1]);i++);
  338.             if(Options[i].ch=='\0')
  339.                 option_str[ocount++] = argv[count];
  340.               else
  341.                 (*(Options[i].fun))(&argv[count][2]);
  342.             }
  343.         }
  344.  
  345.     /* Now process the files */
  346.     ctos();
  347.     if(!justs)
  348.        {stoo();
  349.         if(!justc)
  350.             otoexe();
  351.         }
  352.     }
  353.